﻿<?php
	session_start();
	include("classConexionBD.php");
	require("com/practicario/vo/ItemVO.php");

	$conectar=new ConexionBD();
	$conectar->conectar();


	class NewItem
	{
		private $host;
		public $assoc;
		public $itemSelected;
		
		public function insertNewItem($item)
        {
            if ($item['status'] == "save") {
                $status = $item['status'];
                $id = $item['id'];
                $author = $item['author'];
                $title = $item['title'];
                $description = $item['description'];
				$objetives = $item['objetives'];
                $fileAttached = $item['fileAttached'];         
                $id_tp = $item['itemID'];
				$date = date('Y-m-d');
                $pauta = new ItemVO;
			   
                if ($id_tp == -1){
                    mysql_query("INSERT INTO tp_subidos (nombre, observaciones, id_persona,objetivos) VALUES ('".$title."','".$description."','".$id."','".$objetives."')");
                    $id_tp = mysql_insert_id();
					$pauta->id = $id_tp;
                } else {
                    mysql_query("UPDATE tp_subidos SET nombre='".$title."',observaciones='".$description."',objetivos='".$objetives."' WHERE id='".$id_tp."'");
					$pauta->id = $id_tp;
                }
             	
		        foreach($item['fileAttached'] as $fileAttached){
					if ($fileAttached!=""){
						mysql_query("INSERT INTO links (link, fecha, id_tp_subido,id_adjunto) VALUES ('".$fileAttached."','".$date."','".$id_tp."','".$fileAttached."')");
					}
				}
					
	            $pauta->title = $title;
                return $pauta;
               
            } else {       
                return false;
            }
        }
		
		public function publishItem($item){		
					
			foreach($item as $itemPu){
				
				if ($itemPu['itemID'] != -1){
					
					$id = $itemPu['id'];
					$receiver = $itemPu['receiver'];
					$dueDate = $itemPu['dueDate'];
					$itemId = $itemPu['itemID'];
					$date = date('Y-m-d');
					
				
					$cadena = "observaciones a cargar";	
			
					$result = mysql_query("SELECT nombre,observaciones,objetivos FROM tp_subidos where id = '".$itemId."'");
					while ($row = mysql_fetch_assoc($result)) {
						$title = $row['nombre'];
						$description = $row['observaciones'];
						$objetives = $row['objetivos'];
					}
				
					mysql_query("INSERT INTO asignacion (id_cursa, id_tp_subido, fecha_alta, fecha_baja, observaciones, _nombre, _observaciones, _objetivos) VALUES ('".$receiver."','".$itemId."','".$date."','".$dueDate."','".$cadena."','".$title."','".$description."','".$objetives."')");								
						
					
				}
			}				
		}
		
		public function searchMyItems($id, $userType)
		{
			$matriz = array();
			$matriz[] = array('id' => 1, 'name' => 'Pauta Flex');
			$matriz[] = array('id' => 2, 'name' => 'Pauta AIR');
			$matriz[] = array('id' => 3, 'name' => 'Pauta Java');
			$matriz[] = array('id' => 4, 'name' => 'Pauta C++');
			$matriz[] = array('id' => 5, 'name' => 'Pauta Perl');
			
			return $matriz;
		}
		
		public function deleteItem($item)
		{
			
			if (isset($item)){
				//no se borran mas los datos, se duplica en otra tabla.
				mysql_query("DELETE FROM links WHERE id_tp_subido ='".$item."'");
				mysql_query("DELETE FROM tp_subidos WHERE id ='".$item."'");
				$value = true;
				return $value;
			}else{
				return false;
			}
			
		}
		
		public function getSelectedItem($userID, $itemID)
		{
			//$result = mysql_query("SELECT tp_subidos.*,links.link FROM tp_subidos LEFT JOIN links ON tp_subidos.id=links.id_tp_subido where tp_subidos.id_persona='".$userID."' and tp_subidos.id='".$itemID."'");
			$result = mysql_query("SELECT tp_subidos.* FROM tp_subidos where tp_subidos.id_persona='".$userID."' and tp_subidos.id='".$itemID."'");
			$result2 = mysql_query("SELECT links.* FROM links where id_tp_subido='".$itemID."'");
			while ($row = mysql_fetch_assoc($result)) {
				$itemSelected = new ItemVO();
				$itemSelected->id = $row['id'];
				$itemSelected->author = $row['nombre'];
				$itemSelected->title = $row['nombre'];
				$itemSelected->status = $row['id'];
				$itemSelected->description = $row['observaciones'];
				$itemSelected->objetives = $row['objetivos'];
				while ($row = mysql_fetch_assoc($result2)) {
					$itemSelected->fileAttached[] = $row['link'];
				}
			}	
			return $itemSelected;
		}
		
		

}


?>